package com.young.coordin.server.dao.impl;

import com.young.common.core.dal.service.IDataAccessService;
import com.young.coordin.server.dao.ICoordinRegisterDao;
import com.young.coordin.server.model.RegisterDO;
import com.young.interfaces.log.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 协调中心注册dao
 * Created by Admin on 2020/12/13.
 */
@Component
public class CoordinRegisterDaoImpl implements ICoordinRegisterDao {

    private static final String MAPPER = "com.young.coordin.mapper.register";

    /**
     * 数据服务
     */
    @Autowired
    IDataAccessService dataAccessService;

    /**
     * 插入注册信息
     * @param registerDO 注册信息
     * @return 插入数量
     */
    @Log("插入注册信息")
    @Override
    public int insert(RegisterDO registerDO) {
        return dataAccessService.insert(MAPPER + ".insert", registerDO);
    }

    /**
     * 更新心跳版本号
     *
     * @param clientIp   客户端ip
     * @param clientPort 客户端端口
     * @param newVersion 新心跳版本号
     * @return
     */
    @Log("更新心跳版本号")
    @Override
    public int updateHbVersion(String clientIp, Integer clientPort, long newVersion) {
        RegisterDO registerDO = new RegisterDO();
        registerDO.setClientIp(clientIp);
        registerDO.setClientPort(clientPort);
        registerDO.setHeartbeatVersion(newVersion);
        return dataAccessService.update(MAPPER + ".updateHbVersion", registerDO);
    }

    /**
     * 获取客户端注册信息
     *
     * @param clientIp   客户端ip
     * @param clientPort 客户端端口号
     * @return 注册信息
     */
    @Log("获取客户端注册信息")
    @Override
    public RegisterDO getClientRegisterInfo(String clientIp, Integer clientPort) {
        RegisterDO registerDO = new RegisterDO();
        registerDO.setClientIp(clientIp);
        registerDO.setClientPort(clientPort);
        return dataAccessService.getObject(MAPPER + ".getClientRegisterInfo", registerDO);
    }
}
